package tr.edu.yildiz.ce.dap.appagent.service;

import java.io.File;
import java.io.FileInputStream;

import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.xml.sax.InputSource;
import org.xml.sax.XMLReader;

import tr.edu.yildiz.ce.dap.appagent.model.XMLModel;
import tr.edu.yildiz.ce.dap.appagent.util.AppAgentHandler;

public class DapXmlConverter {
        
    public static String convertToXML(Object obj) throws Exception{
        return ((XMLModel)obj).toXML();
    }
    
	public static Object convertToObject(File file) throws Exception{
		SAXParserFactory spf = SAXParserFactory.newInstance();
		SAXParser sp = spf.newSAXParser();
		XMLReader xr = sp.getXMLReader();
		AppAgentHandler myXMLHandler = new AppAgentHandler();
		xr.setContentHandler(myXMLHandler);
		xr.parse(new InputSource(new FileInputStream(file)));
		return myXMLHandler.getAppDefinition();
	}
    
}
